library(ggplot2)
library(Cairo)
library(foreign)
library(gridExtra)
library(gtable)
library(scales)
library(data.table)
library(dplyr)
library(plyr)
library(openxlsx)
sessionInfo()
library(readstata13)
Sys.setlocale(,"CHS")
library(reshape2)
library(wesanderson)


setwd("D:/Dropbox/TF/replication")

listdele<-function(x){
  a<-x[complete.cases(x),]
  return(a)
}

theme_set(theme_bw() + theme(legend.position="bottom",
                             legend.direction="horizontal",
                             axis.title.x=element_text(vjust=-.5),
                             axis.text.y =  element_text(colour = "black", hjust = 0 , vjust=.5 ,size=11),
                             title=element_text(face="bold",vjust=1.5),
                             strip.text=element_text(size=13),
                             plot.title = element_text(hjust = 0.5)))


pd<-position_dodge(width=0.4)
cl<-c("steelblue","orange")

############################################
##### Attitude toward Politics  ############
############################################


##### by topics
d<-listdele(read.xlsx('Data_Figure.xlsx',sheet=1))
d$qlab<-"Results by Subsample"
d$cohort<-c("Housing/Neighborhood Management","Urban Transportation","Public Security",
            "Environment","Labor Dispute","Infrastructure","Land Expropriation",
            "Commerce","Social Welfare","Health/Education","Employment","Corruption",
            "Agriculture/Rural Area","Household Registration (Hukou)")

d$cohort<-factor(d$cohort,levels=c("Household Registration (Hukou)","Agriculture/Rural Area","Corruption","Employment",
                                   "Health/Education","Social Welfare","Commerce",
                                   "Land Expropriation","Infrastructure","Labor Dispute",
                                   "Environment","Public Security","Urban Transportation","Housing/Neighborhood Management"))  

gatt<-ggplot(d,aes(x=beta,y=cohort))+
  geom_pointrange(aes(xmax=ub95,xmin=lb95),position=pd)+facet_wrap(~qlab,ncol=1,scales="free_x")+
  geom_vline(aes(xintercept=0), linetype="dashed",color="red")+
  theme(axis.text.y=element_text(hjust = 1))+
  scale_color_manual("",values = wes_palette("GrandBudapest1"))+
  scale_shape_discrete("")+
  ylab("Topics")+xlab("Estimated Effect of Third Front on Petitions by Topics")
pdf("bytopics.pdf",width=8,height=6)
gatt
dev.off()



##### by year
mats<-grep("coef_plot",getSheetNames("Data_Figure.xlsx"))
qlab<-c("1. Nationwide","2. With the Third Front")
dlist<-list()
for(i in 1:length(mats)){
  d<-listdele(read.xlsx('Data_Figure.xlsx',sheet=mats[i]))
  d$qlab<-qlab[i]
  d$cohort<-factor(c("2008","2009","2010","2011","2012","2013","2014","2015","2016","2017","2018"),levels=c("2008","2009","2010","2011","2012","2013","2014","2015","2016","2017","2018"))
  dlist[[i]]<-d
}

dx<-rbindlist(dlist)


gatt<-ggplot(dx,aes(x=cohort,y=beta))+
  geom_pointrange(aes(ymin=lb95,ymax=ub95),position=pd)+facet_wrap(~qlab,ncol=1,scales="free_y")+
  geom_hline(yintercept=0, linetype="dashed",color="red")+
  scale_color_manual("",values = wes_palette("GrandBudapest1"))+
  scale_shape_discrete("")+
  xlab("By Years")+ylab("Estimated effect of Third Front on Petitions")

pdf("byyear.pdf",width=8,height=6)
gatt
dev.off()


##### plot_ACC
mats<-grep("polatt",getSheetNames("Data_Figure.xlsx"))
qlab<-c("1. I know more about China's\nimportant political problems\nthan others","2. I know more about politics\nthan an average person", "3. I think I am fully capable of\nparticipating in politics", "4. I often talk to others about politics","5. Participation preference:\ngovernment", "6. Participation preference:\nsocial organization")
dlist<-list()
for(i in 1:length(mats)){
  d<-listdele(read.xlsx('Data_Figure.xlsx',sheet=mats[i]))
  d$qlab<-qlab[i]
  #d$loc<-factor(rep(c("Non-Third Front","Third Front"),each=5),levels=c("Non-Third Front","Third Front"))
  d$cohort<-factor(c("1940s or\nbefore","1950s","1960s","1970s","1980s or\nlater"),levels=c("1940s or\nbefore","1950s","1960s","1970s","1980s or\nlater"))
  dlist[[i]]<-d
}

dx<-rbindlist(dlist)


gatt<-ggplot(dx,aes(x=cohort,y=beta))+
  geom_pointrange(aes(ymin=lb95,ymax=ub95),position=pd)+facet_wrap(~qlab,ncol=2,scales="free_y")+
  geom_hline(yintercept=0, linetype="dashed",color="red")+
  scale_color_manual("",values = wes_palette("GrandBudapest1"))+
  scale_shape_discrete("")+
  xlab("Birth cohort")+ylab("Estimated effect of Third Front")

pdf("survey_att.pdf",width=8,height=6)
gatt
dev.off()


##### plot_CMDS
mats<-grep("plot_",getSheetNames("Data_Figure.xlsx"))
qlab<-c("1. Making suggestions to\none's own workplace, village,\nor neighborhood","2. Making suggestions\nto government", "3. Participating in\nparty and league activities")
dlist<-list()
for(i in 1:length(mats)){
  d<-listdele(read.xlsx('Data_Figure.xlsx',sheet=mats[i]))
  d$qlab<-qlab[i]
  #d$loc<-factor(rep(c("Non-Third Front","Third Front"),each=5),levels=c("Non-Third Front","Third Front"))
  d$cohort<-factor(c("1950s","1960s","1970s","1980s or\nlater"),levels=c("1950s","1960s","1970s","1980s or\nlater"))
  dlist[[i]]<-d
}

dx<-rbindlist(dlist)


gatt<-ggplot(dx,aes(x=cohort,y=beta))+
  geom_pointrange(aes(ymin=lb95,ymax=ub95),position=pd)+facet_wrap(~qlab,ncol=3,scales="free_y")+
  geom_hline(yintercept=0, linetype="dashed",color="red")+
  scale_color_manual("",values = wes_palette("GrandBudapest1"))+
  scale_shape_discrete("")+
  xlab("Birth cohort")+ylab("Estimated effect of Third Front")

pdf("survey_migrants1.pdf",width=10,height=4)
gatt
dev.off()

